<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 9/27/12
 * Time: 10:44 AM
 * To change this template use File | Settings | File Templates.
 */
class Libs_Bootstrap
{
    public  function __construct()
    {
        $this->delegate();
    }
    public function parseUrl(&$controller,&$action,&$param=null)
    {
        if(isset($_GET['url']))
        {
            $arr_url=explode('/',rtrim($_GET['url'],'/'));
            if(file_exists('controllers/'.$arr_url[0].'.php'))
            {
                $controller=$arr_url[0];
                array_shift($arr_url);

            }
            else
            {
                $controller='Index';
                $action='index';
            }
            if(isset($arr_url[0]))
            {
                $class='Controllers_'.$controller;
                if(is_callable(array($class,$arr_url[0])))
                {
                    $action=$arr_url[0];
                    array_shift($arr_url);
                }
                else
                {
                    $action='index';

            }
                if(count($arr_url)>0)
                {
                    $param=$arr_url;
                }
            }
            else
            {
                $action='index';
            }
        }
            else
            {
                $controller='index';
                $action='index';
            }
        }
    public function delegate()
    {
        $this->parseUrl($controller,$action,$param);
        $class="Controllers_".$controller;
        $controller=new $class();
        $controller->$action($param,$action);
    }
}
